package chapter06;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Test03_HashMap {
    public static void main(String[] args) {
        HashMap hashMap = new HashMap();
        //增加元素 hashMap是无序的
        hashMap.put(1,"张三");
        hashMap.put(3,"李四");
        hashMap.put(2,"王五");
        System.out.println(hashMap);
        //如果增加重复的key 会直接替换对应的value 更新值
        hashMap.put(1,"田七");
        System.out.println(hashMap);
        //查询
        System.out.println(hashMap.containsKey(1));
        System.out.println(hashMap.containsValue("田七"));
        System.out.println(hashMap.get(1));
        //删除
        hashMap.remove(1);
        System.out.println(hashMap);
        //遍历
        //使用迭代器
        Set set = hashMap.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            Map.Entry set1 =  (Map.Entry)iterator.next();
            int key = (int)set1.getKey();
            String value = (String)set1.getValue();
            System.out.println(key+":"+value);
        }
        //使用foreach
        for(Object e:hashMap.entrySet()){
            Map.Entry e1 = (Map.Entry) e;
            System.out.println(e1.getKey());
            System.out.println(e1.getValue());
        }
        //abdsdkflseorip 统计字符串中出现字符的个数
    }
}
